﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query wrapper for the thrift call multiget_slice for subcolumns  of supercolumns
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN">supercolumn type</typeparam>
    /// <typeparam name="N">name type</typeparam>
    /// <typeparam name="V">value type</typeparam>
    public class ThriftMultigetSubSliceQuery<K, SN, N, V> : AbstractSliceQuery<K, N, V, Rows<K, N, V>>, MultigetSubSliceQuery<K, SN, N, V>
    {
        public ThriftMultigetSubSliceQuery(Keyspace k, Serializer<K> keySerializer, Serializer<SN> sNameSerializer,
            Serializer<N> nameSerializer, Serializer<V> valueSerializer) : base(k, keySerializer, nameSerializer, valueSerializer)
        {
            if (nameSerializer == null)
            {
                throw new NullReferenceException("Name serializer cannot be null");
            }
            SuperNameSerializer = sNameSerializer;
        }
        public List<K> Keys
        {
            get;set;
        }
        public Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        public SN SuperColumn
        {
            get;set;
        }
        public MultigetSubSliceQuery<K, SN, N, V> SetKeys(List<K> keys)
        {
            Keys = keys;
            return this;
        }
        public MultigetSubSliceQuery<K, SN, N, V> SetSuperColumn(SN superColumn)
        {
            if (superColumn == null)
            {
                throw new NullReferenceException("SuperColumn cannot be null");
            }
            SuperColumn = superColumn;
            return this;
        }
        public new MultigetSubSliceQuery<K, SN, N, V> SetColumnFamily(string cf)
        {
            return (MultigetSubSliceQuery<K, SN, N, V>) base.SetColumnFamily(cf);
        }
        public new MultigetSubSliceQuery<K, SN, N, V> SetRange(N start, N finish, bool reversed, int count)
        {
            return (MultigetSubSliceQuery<K, SN, N, V>) base.SetRange(start, finish, reversed, count);
        }
        public new MultigetSubSliceQuery<K, SN, N, V> SetColumnNames(List<N> columnNames)
        {
            return (MultigetSubSliceQuery<K, SN, N, V>) base.SetColumnNames(columnNames);
        }
        public override string ToString()
        {
            return "MultigetSubSliceQuery(" + SuperColumn + "," + Keys + "," +
                base.ToStringInternal() + ")";
        }
        public override QueryResult<Rows<K, N, V>> Execute()
        {
            if (ColumnFamilyName == null || Keys == null || SuperColumn == null)
            {
                throw new NullReferenceException("SuperColumn, ColumnFamily name and Keys cannot be null");
            }
            Func<KeyspaceService, Rows<K,N,V>> x = (KeyspaceService ks) =>
            {
                List<byte[]> keylist = new List<byte[]>();
                foreach (K key in Keys)
                {
                    if (key != null)
                    {
                        keylist.Add(KeySerializer.ToBytes(key));
                    }
                }
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                columnParent.Super_column = SuperNameSerializer.ToBytes(SuperColumn);
                Dictionary<K, List<Column>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.MultigetSlice(keylist, columnParent, GetPredicate()));
                return new RowsImpl<K, N, V>(thriftRet, ColumnNameSerializer, ValueSerializer);
            };
            return new QueryResultImpl<Rows<K,N,V>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<Rows<K,N,V>>(x)), this);
        }
    }
}
